using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameSavingPeople : MonoBehaviour
{
	public static GameSavingPeople scene;

	public Transform playerContainer;

	public float timeCrowd = 120f;

	public float wallRoom = 10f;

	public string sceneComplete;

	public ScreenTitleLevel titleLevel;

	public Text scoresCurrent;

	public ZombieCharacteristic aliveEnityBehaviour;

	private ManagerZombies[] managerZombies = new ManagerZombies[0];

	private float[] startMinSpeed = new float[0];

	private Vector3 startPosition = default(Vector3);

	private float timerLoadScene;

	private ZombieInHome deadPerson;

	private CameraGame gameCamera;

	private float timerCamera = 1.5f;

	private Vector3 startCameraPosition = default(Vector3);

	private Vector3 targetCameraPosition = default(Vector3);

	private float startCameraSize;

	private float targetCameraSize = 4.5f;

	private float startTimeScale;

	private float targetTimeScale = 0.25f;

	private Canvas canvas;

	private int numAliveScores;

	private float scaleTimer = 1f;

	private float currentTimerCrowd;

	private float markDistTimer;

	private void OnDrawGizmos()
	{
		if (base.enabled)
		{
			Gizmos.color = new Color(0f, 1f, 0.5f);
			Vector3 position = base.transform.position;
			Gizmos.DrawLine(position, new Vector3(position.x + wallRoom, position.y + 3f, position.y));
			Gizmos.DrawLine(new Vector3(position.x + wallRoom, position.y - 2f, position.y), new Vector3(position.x + wallRoom, position.y + 5f, position.y));
		}
	}

	private void Start()
	{
		scene = this;
		base.transform.SetParent(playerContainer);
		startPosition = base.transform.position;
		gameCamera = UnityEngine.Object.FindObjectOfType<CameraGame>();
		canvas = UnityEngine.Object.FindObjectOfType<Canvas>();
		managerZombies = base.gameObject.GetComponents<ManagerZombies>();
		startMinSpeed = new float[managerZombies.Length];
		for (int i = 0; i < managerZombies.Length; i++)
		{
			startMinSpeed[i] = managerZombies[i].scaleSpeedMove;
		}
		InvokeRepeating("CheckAlive", 10f, 4f);
		Player.listenersDeath = OnGameOver;
		titleLevel.Disable();
	}

	private void Update()
	{
		if (Time.timeScale != 0f)
		{
			currentTimerCrowd += Time.deltaTime / Time.timeScale;
			scaleTimer = Mathf.Clamp01((timeCrowd - currentTimerCrowd) / timeCrowd);
			float a = scaleTimer;
			Vector3 position = base.transform.position;
			scaleTimer = Mathf.Min(a, 1f - Mathf.Clamp01((position.x - startPosition.x) / wallRoom));
		}
		if (currentTimerCrowd - markDistTimer > 0.2f)
		{
			markDistTimer = currentTimerCrowd;
			for (int i = 0; i < managerZombies.Length; i++)
			{
				managerZombies[i].periodScale = scaleTimer;
				managerZombies[i].scaleSpeedMove = Mathf.Lerp(managerZombies[i].scaleSpeedMoveMax, startMinSpeed[i], scaleTimer);
			}
		}
		if (deadPerson != null && Time.timeScale != 0f)
		{
			timerCamera -= Time.deltaTime / Time.timeScale;
			timerCamera = Mathf.Max(0f, timerCamera);
			targetCameraPosition = deadPerson.body.transform.position;
			targetCameraPosition.z = startCameraPosition.z;
			Camera.main.transform.position = Vector3.Lerp(targetCameraPosition, startCameraPosition, timerCamera / 1.5f);
			Camera.main.orthographicSize = Mathf.Lerp(targetCameraSize, startCameraSize, timerCamera / 1.5f);
			gameCamera.ClampPosition(Camera.main);
			Time.timeScale = Mathf.Lerp(targetTimeScale, startTimeScale, timerCamera / 1.5f);
		}
		if (timerLoadScene != 0f)
		{
			timerLoadScene -= Time.deltaTime / Time.timeScale;
			if (timerLoadScene <= 0f)
			{
				LoadScene();
			}
		}
	}

	public void CheckAlive()
	{
		if (ManagerZombies.aliveSceneTotal == 0)
		{
			CancelInvoke("CheckAlive");
			titleLevel.ShowCompelete(LoadScene);
		}
	}

	public void OnDeathPeople(ZombieInHome zombie)
	{
		if (!(deadPerson != null))
		{
			deadPerson = zombie;
			startCameraPosition = Camera.main.transform.position;
			startCameraSize = Camera.main.orthographicSize;
			startTimeScale = Time.timeScale;
			canvas.gameObject.SetActive(value: false);
			OnGameOver();
		}
	}

	public void OnGameOver()
	{
		gameCamera.enabled = false;
		CancelInvoke("CheckAlive");
		timerLoadScene = 3f;
		for (int i = 0; i < managerZombies.Length; i++)
		{
			managerZombies[i].enabled = false;
		}
	}

	public void OnSavedPerson()
	{
		numAliveScores++;
		scoresCurrent.text = numAliveScores.ToString();
		ManagerZombies.aliveSceneTotal = Mathf.Max(0, ManagerZombies.aliveSceneTotal - 1);
	}

	public void LoadScene()
	{
		if (startTimeScale != 0f)
		{
			Time.timeScale = startTimeScale;
		}
		SettingsStartGame.ResultGame(ManagerZombies.kills, numAliveScores, Mathf.CeilToInt(Player.onStage.moneyHits));
		SettingsStartGame.GameOver(resetWeapon: false);
		scene = null;
		SceneManager.LoadScene(sceneComplete);
	}
}
